package test21.upload;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class TCPFileUploadClient {
    public static void main(String[] args) throws IOException {
        //客户端连接服务端 8888，得到Socket对象
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
        //创建读取磁盘文件的输入流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\资料\\test.png"));
        //bytes 就是filePath对应的字节数组
        byte[] array = StreamUtils.streamToByteArray(bufferedInputStream);

        //通过socket获取到输出流, 将bytes数据发送给服务端
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
        //将文件对应的字节数组的内容，写入到数据通道
        bufferedOutputStream.write(array);
        bufferedOutputStream.flush();
        socket.shutdownOutput();

        //接收从服务端回复的消息
        InputStream inputStream = socket.getInputStream();
        //使用StreamUtils 的方法，直接将 inputStream 读取到的内容 转成字符串
        String s = StreamUtils.streamToString(inputStream);
        System.out.println(s);

        //关闭相关的流
        bufferedOutputStream.close();
        bufferedInputStream.close();
        socket.close();

    }
}
